cmake_minimum_required(VERSION 3.6)
project(moonlight-embedded VERSION 2.7.1 LANGUAGES C)
SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
SET(CMAKE_C_STANDARD 99)
include(${CMAKE_ROOT}/Modules/GNUInstallDirs.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/generate_version_header.cmake)

include(CheckCSourceCompiles)

add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wno-pointer-sign -Wno-sign-compare -Wno-switch)

aux_source_directory(./src SRC_LIST)
list(APPEND SRC_LIST ./src/input/evdev.c ./src/input/mapping.c ./src/input/udev.c)

set(MOONLIGHT_DEFINITIONS)

find_package(ALSA)
find_package(Opus REQUIRED)
find_package(Broadcom-OMX)
find_package(Freescale)
find_package(Amlogic)
find_package(Rockchip)

find_package(PkgConfig REQUIRED)

option(ENABLE_SDL "Compile SDL support" ON)
option(ENABLE_FFMPEG "Compile FFMPEG support" ON)
option(ENABLE_X11 "Compile X11 support (requires ENABLE_FFMPEG)" ON)
option(ENABLE_CEC "Compile CEC support" ON)
option(ENABLE_PULSE "Compile PulseAudio support" ON)

pkg_check_modules(EVDEV REQUIRED libevdev)
pkg_check_modules(UDEV REQUIRED libudev)
if (ENABLE_SDL)
  pkg_check_modules(SDL sdl2>=2.0.4)
endif()
if (ENABLE_FFMPEG)
  pkg_check_modules(AVCODEC libavcodec)
  pkg_check_modules(AVUTIL libavutil)
  pkg_check_modules(VDPAU vdpau)
  pkg_check_modules(LIBVA libva)
  pkg_check_modules(EGL egl)
  pkg_check_modules(GLES glesv2)

  if (ENABLE_X11)
    pkg_check_modules(XLIB x11)
    pkg_check_modules(LIBVA_X11 libva-x11)
  endif()
endif()
if (ENABLE_PULSE)
  pkg_check_modules(PULSE libpulse-simple)
endif()
if (ENABLE_CEC)
  pkg_check_modules(CEC libcec>=4)
endif()

pkg_check_modules(MMAL mmal)
if (NOT MMAL_FOUND)
  find_package(MMAL)
endif()

set(VDPAU_ACCEL_FOUND FALSE)
set(VA_ACCEL_FOUND FALSE)
set(SOFTWARE_FOUND FALSE)

if(AVCODEC_FOUND AND AVUTIL_FOUND)
  if(EGL_FOUND AND GLES_FOUND AND XLIB_FOUND)
    set(X11_FOUND TRUE)
    if(VDPAU_FOUND)
      set(VDPAU_ACCEL_FOUND TRUE)
    endif()
    if (LIBVA_FOUND AND LIBVA_X11_FOUND)
      set(VA_ACCEL_FOUND TRUE)
    endif()
  endif()
  if(SDL_FOUND OR X11_FOUND)
    set(SOFTWARE_FOUND TRUE)
  endif()
endif()

SET(MOONLIGHT_COMMON_INCLUDE_DIR ./third_party/moonlight-common-c/src)
SET(GAMESTREAM_INCLUDE_DIR ./libgamestream)

include_directories("${PROJECT_BINARY_DIR}")

add_subdirectory(libgamestream)

add_executable(moonlight ${SRC_LIST})
target_link_libraries(moonlight m)
target_link_libraries(moonlight gamestream)

if (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  set(ALSA_FOUND FALSE)
  target_sources(moonlight PRIVATE ./src/audio/oss.c)
endif()

check_c_source_compiles("#include <sys/auxv.h>
                         int main(void) { return getauxval(AT_HWCAP); }" HAVE_GETAUXVAL)
if (HAVE_GETAUXVAL)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_GETAUXVAL)
endif()

check_c_source_compiles("int main(void) { return __builtin_cpu_supports(\"aes\"); }" HAVE_BICS_AES)
if (HAVE_BICS_AES)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_BICS_AES)
endif()

if (CEC_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_LIBCEC)
  list(APPEND MOONLIGHT_OPTIONS CEC)
  target_sources(moonlight PRIVATE ./src/input/cec.c)
  target_include_directories(moonlight PRIVATE ./third_party/libcec ${CEC_INCLUDE_DIRS})
  target_link_libraries(moonlight ${CEC_LIBRARIES})
endif()

if(AMLOGIC_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_AML)
  list(APPEND MOONLIGHT_OPTIONS AML)
  add_library(moonlight-aml SHARED ./src/video/aml.c ./src/util.c ${ILCLIENT_SRC_LIST})
  target_include_directories(moonlight-aml PRIVATE ${AMLOGIC_INCLUDE_DIRS} ${GAMESTREAM_INCLUDE_DIR} ${MOONLIGHT_COMMON_INCLUDE_DIR})
  target_link_libraries(moonlight-aml gamestream ${AMLOGIC_LIBRARIES})
  set_property(TARGET moonlight-aml PROPERTY COMPILE_DEFINITIONS ${AMLOGIC_DEFINITIONS})
  install(TARGETS moonlight-aml DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if(BROADCOM-OMX_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_PI)
  list(APPEND MOONLIGHT_OPTIONS PI)
  aux_source_directory(./third_party/ilclient ILCLIENT_SRC_LIST)
  add_library(moonlight-pi SHARED ./src/video/pi.c ./src/audio/omx.c ./src/util.c ${ILCLIENT_SRC_LIST})
  target_include_directories(moonlight-pi PRIVATE ./third_party/ilclient ${BROADCOM_INCLUDE_DIRS} ${GAMESTREAM_INCLUDE_DIR} ${MOONLIGHT_COMMON_INCLUDE_DIR} ${OPUS_INCLUDE_DIRS})
  target_link_libraries(moonlight-pi gamestream ${BROADCOM_OMX_LIBRARIES} ${OPUS_LIBRARY})
  set_property(TARGET moonlight-pi PROPERTY COMPILE_DEFINITIONS ${BROADCOM_OMX_DEFINITIONS})
  install(TARGETS moonlight-pi DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if(MMAL_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_MMAL)
  list(APPEND MOONLIGHT_OPTIONS MMAL)
  add_library(moonlight-mmal SHARED ./src/video/mmal.c ./src/util.c)
  target_include_directories(moonlight-mmal PRIVATE ${MMAL_INCLUDE_DIRS} ${GAMESTREAM_INCLUDE_DIR} ${MOONLIGHT_COMMON_INCLUDE_DIR})
  target_link_libraries(moonlight-mmal gamestream ${MMAL_LINK_LIBRARIES})
  install(TARGETS moonlight-mmal DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if(FREESCALE_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_IMX)
  list(APPEND MOONLIGHT_OPTIONS IMX)
  add_library(moonlight-imx SHARED ./src/video/imx.c ./src/video/imx_vpu.c ./src/util.c)
  target_include_directories(moonlight-imx PRIVATE ${FREESCALE_INCLUDE_DIRS} ${GAMESTREAM_INCLUDE_DIR} ${MOONLIGHT_COMMON_INCLUDE_DIR})
  target_link_libraries(moonlight-imx gamestream ${FREESCALE_LIBRARIES})
  install(TARGETS moonlight-imx DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if(ROCKCHIP_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_ROCKCHIP)
  list(APPEND MOONLIGHT_OPTIONS ROCKCHIP)
  add_library(moonlight-rk SHARED ./src/video/rk.c ./src/util.c)
  target_include_directories(moonlight-rk PRIVATE ${ROCKCHIP_INCLUDE_DIRS} ${GAMESTREAM_INCLUDE_DIR} ${MOONLIGHT_COMMON_INCLUDE_DIR})
  target_link_libraries(moonlight-rk gamestream ${ROCKCHIP_LIBRARIES})
  set_property(TARGET moonlight-rk PROPERTY COMPILE_DEFINITIONS ${ROCKCHIP_DEFINITIONS})
  install(TARGETS moonlight-rk DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if (SOFTWARE_FOUND)
  target_sources(moonlight PRIVATE ./src/video/ffmpeg.c)
  target_include_directories(moonlight PRIVATE ${AVCODEC_INCLUDE_DIRS} ${AVUTIL_INCLUDE_DIRS})
  target_link_libraries(moonlight ${AVCODEC_LIBRARIES} ${AVUTIL_LIBRARIES})
  if(SDL_FOUND)
    list(APPEND MOONLIGHT_DEFINITIONS HAVE_SDL)
    list(APPEND MOONLIGHT_OPTIONS SDL)
    target_sources(moonlight PRIVATE ./src/video/sdl.c ./src/audio/sdl.c ./src/input/sdl.c)
    target_include_directories(moonlight PRIVATE ${SDL_INCLUDE_DIRS})
    target_link_libraries(moonlight ${SDL_LIBRARIES})
  endif()
  if(X11_FOUND)
    list(APPEND MOONLIGHT_DEFINITIONS HAVE_X11)
    list(APPEND MOONLIGHT_OPTIONS X11)
    target_sources(moonlight PRIVATE ./src/video/x11.c ./src/video/egl.c ./src/input/x11.c)
    target_include_directories(moonlight PRIVATE ${XLIB_INCLUDE_DIRS} ${EGL_INCLUDE_DIRS} ${GLES_INCLUDE_DIRS})
    target_link_libraries(moonlight ${XLIB_LIBRARIES} ${EGL_LIBRARIES} ${GLES_LIBRARIES})
  endif()
  if(VDPAU_ACCEL_FOUND)
    list(APPEND MOONLIGHT_DEFINITIONS HAVE_VDPAU)
    list(APPEND MOONLIGHT_OPTIONS VDPAU)
    target_include_directories(moonlight PRIVATE ${VDPAU_INCLUDE_DIRS})
    target_link_libraries(moonlight ${VDPAU_LIBRARIES})
  endif()
  if(VA_ACCEL_FOUND)
    list(APPEND MOONLIGHT_DEFINITIONS HAVE_VAAPI)
    list(APPEND MOONLIGHT_OPTIONS VAAPI)
    target_sources(moonlight PRIVATE ./src/video/ffmpeg_vaapi.c)
    target_include_directories(moonlight PRIVATE ${LIBVA_INCLUDE_DIRS} ${LIBVA_X11_INCLUDE_DIRS})
    target_link_libraries(moonlight ${LIBVA_LIBRARIES} ${LIBVA_X11_LIBRARIES})
  endif()
endif()

if (ALSA_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_ALSA)
  list(APPEND MOONLIGHT_OPTIONS ALSA)
  target_sources(moonlight PRIVATE ./src/audio/alsa.c)
  target_include_directories(moonlight PRIVATE ${ALSA_INCLUDE_DIR})
  target_link_libraries(moonlight ${ALSA_LIBRARY})
endif()

if (PULSE_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_PULSE)
  list(APPEND MOONLIGHT_OPTIONS PULSE)
  target_sources(moonlight PRIVATE ./src/audio/pulse.c)
  target_include_directories(moonlight PRIVATE ${PULSE_INCLUDE_DIRS})
  target_link_libraries(moonlight ${PULSE_LIBRARIES})
endif()

if (AMLOGIC_FOUND OR BROADCOM-OMX_FOUND OR MMAL_FOUND OR FREESCALE_FOUND OR ROCKCHIP_FOUND OR X11_FOUND)
  list(APPEND MOONLIGHT_DEFINITIONS HAVE_EMBEDDED)
  list(APPEND MOONLIGHT_OPTIONS EMBEDDED)
endif()

if(NOT AMLOGIC_FOUND AND NOT BROADCOM-OMX_FOUND AND NOT MMAL_FOUND AND NOT FREESCALE_FOUND AND NOT ROCKCHIP_FOUND AND NOT SOFTWARE_FOUND)
  message(FATAL_ERROR "No video output available")
endif()

configure_file("./src/configuration.h.in" "${PROJECT_BINARY_DIR}/configuration.h")

set_property(TARGET moonlight PROPERTY COMPILE_DEFINITIONS ${MOONLIGHT_DEFINITIONS})
target_include_directories(moonlight PRIVATE ${GAMESTREAM_INCLUDE_DIR} ${MOONLIGHT_COMMON_INCLUDE_DIR} ${OPUS_INCLUDE_DIRS} ${EVDEV_INCLUDE_DIRS} ${UDEV_INCLUDE_DIRS})
target_link_libraries(moonlight ${EVDEV_LIBRARIES} ${OPUS_LIBRARY} ${UDEV_LIBRARIES} ${CMAKE_DL_LIBS})

add_subdirectory(docs)

install(TARGETS moonlight DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES ./third_party/SDL_GameControllerDB/gamecontrollerdb.txt DESTINATION ${CMAKE_INSTALL_DATADIR}/moonlight)
install(FILES moonlight.conf DESTINATION ${CMAKE_INSTALL_SYSCONFDIR})
